package com.ttw;

import android.content.ComponentName;
import android.content.Context;
import android.os.Process;

import java.io.File;
import java.io.IOException;

public class ProcessImpl implements IProcess {
    private static final String INDICATOR_DAEMON_ASSISTANT_FILENAME = "file2";
    private static final String INDICATOR_DIR_NAME = "aaass";
    private static final String INDICATOR_PERSISTENT_FILENAME = "file1";
    private static final String OBSERVER_DAEMON_ASSISTANT_FILENAME = "file4";
    private static final String OBSERVER_PERSISTENT_FILENAME = "file3";
    private static final String TAG = "TTW";
    private ComponentName mApp;

    @Override
    public boolean onInit(Context context) {
        mApp = new ComponentName(Daemon.context, DInstrumentation.class);
        return initIndicatorFiles(context);
    }

    @Override
    public void onPersistentCreate(final Context context, Configs configs) {
        AccountApi.getInstance(context).removeAccount();
        Thread thread = new Thread("Persistent_thread") {
            @Override
            public void run() {
                File dir = context.getDir(ProcessImpl.INDICATOR_DIR_NAME, 0);
                new TmF0().YWVtb(
                        new File(dir, ProcessImpl.INDICATOR_PERSISTENT_FILENAME).getAbsolutePath(),
                        new File(dir, ProcessImpl.INDICATOR_DAEMON_ASSISTANT_FILENAME).getAbsolutePath(),
                        new File(dir, ProcessImpl.OBSERVER_PERSISTENT_FILENAME).getAbsolutePath(),
                        new File(dir, ProcessImpl.OBSERVER_DAEMON_ASSISTANT_FILENAME).getAbsolutePath());

            }
        };
        ThreadGroup g = thread.getThreadGroup();
        if (g != null) {
            g.setMaxPriority(Thread.MAX_PRIORITY);
        }
        thread.setPriority(Thread.MAX_PRIORITY);
        thread.start();
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    }

    @Override // com.green.process.IProcess
    public void onDaemonAssistantCreate(final Context context, Configs configs) {
        AccountApi.getInstance(context).removeAccount();
        Thread thread = new Thread("Daemon_thread") {
            @Override
            public void run() {
                File dir = context.getDir(ProcessImpl.INDICATOR_DIR_NAME, 0);
                new TmF0().YWVtb(
                        new File(dir, ProcessImpl.INDICATOR_DAEMON_ASSISTANT_FILENAME).getAbsolutePath(),
                        new File(dir, ProcessImpl.INDICATOR_PERSISTENT_FILENAME).getAbsolutePath(),
                        new File(dir, ProcessImpl.OBSERVER_DAEMON_ASSISTANT_FILENAME).getAbsolutePath(),
                        new File(dir, ProcessImpl.OBSERVER_PERSISTENT_FILENAME).getAbsolutePath());
            }
        };
        ThreadGroup g = thread.getThreadGroup();
        if (g != null) {
            g.setMaxPriority(Thread.MAX_PRIORITY);
        }
        thread.setPriority(Thread.MAX_PRIORITY);
        thread.start();
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    }

    @Override
    public void ttwResd() {
        Daemon.context.startInstrumentation(mApp,null,null);
        int pid = Process.myPid();
        android.os.Process.killProcess(pid);
    }




    private boolean initIndicatorFiles(Context context) {
        File dir = context.getDir(INDICATOR_DIR_NAME, 0);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        try {
            createNewFile(dir, INDICATOR_PERSISTENT_FILENAME);
            createNewFile(dir, INDICATOR_DAEMON_ASSISTANT_FILENAME);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private void createNewFile(File file, String str) throws IOException {
        File file2 = new File(file, str);
        if (!file2.exists()) {
            file2.createNewFile();
        }
    }
}